﻿using System;

namespace YXYLCommon
{
    [Serializable]
    public class DbDateTime
    {
        public DbDateTime()
        { 
            
        }
        private DbDateTime(object time)
        {
            if (time == Convert.DBNull||Convert.ToString(time)=="")
            {
                this.time = timeDBNull;
                this.timeDBParameter = Convert.DBNull;
            }
            else
            {
                this.time = Convert.ToDateTime(time);
                this.timeDBParameter = Convert.ToDateTime(time);
            }
        }
        private DbDateTime(string time)
        {
            DateTime timeT = new DateTime();
            if (time == string.Empty||time==null||DateTime.TryParse(time,out timeT)==false)
            {
                this.time = timeDBNull;
                this.timeDBParameter = Convert.DBNull;
            }
            else
            {
                //if (time.Length == 8)
                //{
                //    int y, m, d;
                //    int.TryParse(time.Substring(0, 4), out y);
                //    int.TryParse(time.Substring(4, 2), out m);
                //    int.TryParse(time.Substring(6, 2), out d);
                //    if (y != -1 && m != -1 && d != -1)
                //    {
                //        this.time = new DateTime(y, m, d);
                //    }
                //    else
                //    {
                //        this.time = timeDBNull;
                //        this.timeDBParameter = Convert.DBNull;
                //    }
                //}
                //else
                //{
                    this.time = Convert.ToDateTime(time);
                    this.timeDBParameter = Convert.ToDateTime(time);
                //}
            }
        }
        private DbDateTime(DateTime time)
        {
            this.time = time;
            this.timeDBParameter = time;
        }

        public string LongDateString
        {
            get {
                if (this.time == timeDBNull)
                {
                    return string.Empty;
                }
                else
                {
                    return this.time.ToLongDateString();
                } 
            }
        }
        public string DateTimeString
        {
            get
            {
                if (this.time == timeDBNull)
                {
                    return string.Empty;
                }
                else
                {
                    return this.time.ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
        }
        public string LongTimeString
        {
            get
            {
                string str = string.Empty;
                if (this.time != timeDBNull)
                {
                    str = this.time.ToLongTimeString();
                }
                return str;
            }
        }

        public string DateString
        {
            get
            {
                if (this.time == timeDBNull)
                {
                    return string.Empty;
                }
                else
                {
                    return time.ToString("yyyy-MM-dd");
                }
            }
        }
        public string TimeString
        {
            get
            {
                if (this.time == timeDBNull)
                {
                    return string.Empty;
                }
                else
                {
                    return time.ToString("hh:mm:ss");
                }
            }
        }

        private object timeDBParameter = Convert.DBNull;
        private DateTime time=timeDBNull;

        public DateTime Time
        {
            get { return time; }
            set { time = value; }
        }
        /// <summary>
        /// 用于执行存储过程需要的时间类型参数
        /// </summary>
        public object TimeDBParameter
        {
            get { return timeDBParameter; }
        }

        public bool IsDBNull
        {
            get
            {
                if (this.time == timeDBNull)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        /// <summary>
        /// 重写的。时间为空时为空字符，不为空时为DateTime.ToString()
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            string str = string.Empty;
            if (this.time != timeDBNull)
            {
                str = this.time.ToString();
            }
            return str;
        }

        public string ToString(string format)
        {
            if (this.time == timeDBNull)
            {
                return string.Empty;
            }
            else
            {
                return this.time.ToString(format);
            }
        }

        public static DbDateTime Parse(object time)
        {
            return new DbDateTime(time);
        }
        public static DbDateTime Parse(string time)
        {
            return new DbDateTime(time);
        }
        public static DbDateTime Parse(DateTime time)
        {
            return new DbDateTime(time);
        }

        private static readonly DateTime timeDBNull= new DateTime(1753,1,1,11,11,11);
    }

}
